/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.upsam.modelo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author carmas
 */
public class persistencia {
    Connection con = null;
    Statement stat = null;
    ResultSet rs = null;

    void conectar(){
        try{
            con=DriverManager.getConnection("jdbc:derby://localhost:1527/Projects", "admin","admin");
        } catch ( Exception ex){
            Logger.getLogger(persistencia.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ResultSet ejecutarConsulta(String query)
    {

        try {
            conectar();
            stat = con.createStatement();
            rs=stat.executeQuery(query);

        } catch (SQLException ex) {
            Logger.getLogger(persistencia.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public int ejecutarActualizacion(String query)
    {
        int resultado = -999;
        try {
            conectar();
            stat = con.createStatement();
            resultado=stat.executeUpdate(query);
        } catch (SQLException ex) {
            Logger.getLogger(persistencia.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resultado;
    }

    
    public void Cerrar()
    {
        try {
           if (rs != null)
            rs.close();
           if (stat != null)
            stat.close();
           if (con != null)
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(persistencia.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
